const express = require('express');
const router = express.Router();

// 模拟代办列表数据库
let todoList = [];

// 获取所有代办事项
router.get('/todos', (req, res) => {
    try {
        res.json({
            success: true,
            message: '获取代办列表成功',
            data: todoList
        });
    } catch (error) {
        res.status(500).json({
            success: false,
            message: '服务器内部错误',
            error: error.message
        });
    }
});

// 根据 ID 获取单个代办事项
router.get('/todos/:id', (req, res) => {
    const todoId = parseInt(req.params.id);
    const todo = todoList.find(item => item.id === todoId);

    if (!todo) {
        return res.status(404).json({
            success: false,
            message: '未找到该代办事项'
        });
    }

    try {
        res.json({
            success: true,
            message: '获取代办事项成功',
            data: todo
        });
    } catch (error) {
        res.status(500).json({
            success: false,
            message: '服务器内部错误',
            error: error.message
        });
    }
});

// 添加新的代办事项
router.post('/todos', (req, res) => {
    try {
        const { title, description, completed = false } = req.body;

        if (!title) {
            return res.status(400).json({
                success: false,
                message: '标题为必填项'
            });
        }

        const newTodo = {
            id: todoList.length > 0 ? todoList[todoList.length - 1].id + 1 : 1,
            title,
            description,
            completed
        };

        todoList.push(newTodo);

        res.status(201).json({
            success: true,
            message: '代办事项添加成功',
            data: newTodo
        });
    } catch (error) {
        res.status(500).json({
            success: false,
            message: '服务器内部错误',
            error: error.message
        });
    }
});

// 更新代办事项
router.put('/todos/:id', (req, res) => {
    const todoId = parseInt(req.params.id);
    const todoIndex = todoList.findIndex(item => item.id === todoId);

    if (todoIndex === -1) {
        return res.status(404).json({
            success: false,
            message: '未找到该代办事项'
        });
    }

    try {
        const { title, description, completed } = req.body;
        const updatedTodo = {
            ...todoList[todoIndex],
            ...(title && { title }),
            ...(description && { description }),
            ...(completed!== undefined && { completed })
        };

        todoList[todoIndex] = updatedTodo;

        res.json({
            success: true,
            message: '代办事项更新成功',
            data: updatedTodo
        });
    } catch (error) {
        res.status(500).json({
            success: false,
            message: '服务器内部错误',
            error: error.message
        });
    }
});

// 删除代办事项
router.delete('/todos/:id', (req, res) => {
    const todoId = parseInt(req.params.id);
    const todoIndex = todoList.findIndex(item => item.id === todoId);

    if (todoIndex === -1) {
        return res.status(404).json({
            success: false,
            message: '未找到该代办事项'
        });
    }

    try {
        const deletedTodo = todoList.splice(todoIndex, 1)[0];
        res.json({
            success: true,
            message: '代办事项删除成功',
            data: deletedTodo
        });
    } catch (error) {
        res.status(500).json({
            success: false,
            message: '服务器内部错误',
            error: error.message
        });
    }
});

module.exports = router;